/*
 * @(#)AsciiDocsGenerator.java 1.0 2021年02月04日
 *
 *  Copyright (c) 2016, YUNXI. All rights reserved.
 *  YUNXI PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.jimmy.utils.swagger2markup.generate;

import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>Title:AsciiDocsGenerator</p>
 * <p>Description: </p>
 *
 * @author 子曰
 * @date 2021-2-4 14:42
 * @since 0.1.0
 */
public class AsciiDocsGenerator implements IGenerator{
    //输出Ascii格式
    private Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
            .withMarkupLanguage(MarkupLanguage.ASCIIDOC)
            .withOutputLanguage(Language.ZH)
            .withPathsGroupedBy(GroupBy.TAGS)
            .withGeneratedExamples()
            .withoutInlineSchema()
            .build();
    @Override
    public void generateToFolder(String folderPath) throws MalformedURLException {
        //输出Ascii到文件夹
        Swagger2MarkupConverter.from(new URL("http://127.0.0.1:8082/v2/api-docs"))
                .withConfig(config)
                .build()
                .toFolder(Paths.get("./docs/asciidoc/generated"));
    }

    @Override
    public void generateToFile(String url, String filePath) throws MalformedURLException {
        //输出Ascii到单文件
        try{
            Swagger2MarkupConverter.from(new URL(url))
                    .withConfig(config)
                    .build()
                    .toFile(Paths.get(filePath));
        }catch (Exception ex){
            System.err.println("url访问错误："+url);
            ex.printStackTrace();
        }
    }

    @Override
    public void generateToFile(List<String> urlList, List<String> filePathList) throws MalformedURLException {
        for (int i = 0; i < urlList.size(); i++) {
            generateToFile(urlList.get(i), filePathList.get(i));
        }
    }
}
